package com.apobates.jforum2.troll.forum.entity;

import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * 版主
 *
 * @author xiaofanku
 * @since 20200510
 */
@Entity
@Table(name = "apo_board_moderator", uniqueConstraints={@UniqueConstraint(columnNames={"volumesId", "boardId", "memberId", "volumesMaster"})})
public class BoardModerator implements Serializable{
    private static final long serialVersionUID = 2207510275870216006L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    //卷
    private int volumesId;
    //版块|大版主的版块是: 0
    private long boardId;
    //等级
    @Basic
    @Enumerated(EnumType.STRING)
    private ModeratorLevelEnum level;
    //会员ID
    private long memberId;
    //会员的昵称
    private String memberNickname;
    //任命日期
    private LocalDateTime takeDateTime;
    //状态,false(0)禁用,true(1)可用
    @Column(columnDefinition="tinyint(1) default 0")
    private boolean status;
    //是否是大版主,管辖所有版块,true(1)是,false(0)小版主
    @Column(columnDefinition="tinyint(1) default 0")
    private boolean volumesMaster;
    
    //empty constructor for JPA instantiation
    public BoardModerator() {
    }
    
    public BoardModerator(
            int volumesId,
            long boardId,
            ModeratorLevelEnum level,
            long memberId,
            String memberNickname) {
        this.volumesId = volumesId;
        this.boardId = boardId;
        this.level = level;
        this.memberId = memberId;
        this.memberNickname = memberNickname;
        this.takeDateTime = LocalDateTime.now();
        this.status = true;
        this.volumesMaster = false;
    }
    
    public BoardModerator(
            int volumesId,
            long boardId,
            ModeratorLevelEnum level,
            long memberId,
            String memberNickname,
            boolean volumesMaster) {
        this.volumesId = volumesId;
        this.boardId = boardId;
        this.level = level;
        this.memberId = memberId;
        this.memberNickname = memberNickname;
        this.takeDateTime = LocalDateTime.now();
        this.status = true;
        this.volumesMaster = volumesMaster;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (boardId ^ (boardId >>> 32));
        result = prime * result + (int) (memberId ^ (memberId >>> 32));
        result = prime * result + volumesId;
        result = prime * result + (volumesMaster ? 1231 : 1237);
        return result;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BoardModerator other = (BoardModerator) obj;
        if (boardId != other.boardId) {
            return false;
        }
        if (memberId != other.memberId) {
            return false;
        }
        if (volumesId != other.volumesId) {
            return false;
        }
        return volumesMaster == other.volumesMaster;
    }
    @Transient
    public static BoardModerator empty() {
        return new BoardModerator(0, 0L, ModeratorLevelEnum.INTERN, 0L, "");
    }
}